package com.snapan.controller;

import com.snapan.entity.SystemConfig;
import com.snapan.entity.User;
import com.snapan.service.SystemConfigService;
import com.snapan.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/adminconfig")
public class AdminController {
    @Autowired
    private SystemConfigService configService;

    @Autowired
    private UserService userService;


    /**
     * 获取所有系统配置
     */
    @GetMapping("/getconfig")
    public ResponseEntity<Map<String, String>> getAllConfig() {
        Map<String, String> configMap = configService.getAllConfigMap();
        return ResponseEntity.ok(configMap);
    }


    /**
     * 更新系统配置接口
     * 接收一个键值对Map，根据配置键更新对应的配置值
     */
    @PostMapping("/updateconfig")
    public ResponseEntity<Map<String, Object>> updateConfig(@RequestBody Map<String, String> configUpdates) {
        Map<String, Object> response = new HashMap<>();

        try {
            // 获取所有现有配置
            List<SystemConfig> allConfigs = configService.getAll();
            Map<String, SystemConfig> configMap = new HashMap<>();

            // 创建配置键到配置对象的映射
            for (SystemConfig config : allConfigs) {
                configMap.put(config.getConfigKey(), config);
            }

            int updatedCount = 0;

            // 遍历要更新的配置
            for (Map.Entry<String, String> entry : configUpdates.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();

                // 检查配置是否存在
                if (configMap.containsKey(key)) {
                    SystemConfig config = configMap.get(key);
                    config.setConfigValue(value);

                    // 更新配置
                    if (configService.update(config)) {
                        updatedCount++;
                    }
                }
            }

            response.put("success", true);
            response.put("message", "成功更新 " + updatedCount + " 个配置项");
            response.put("updatedCount", updatedCount);

            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "更新配置时发生错误: " + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }
    }

    /**
     * 获取用户信息
     *
     */
    @GetMapping("/getusers")
    public ResponseEntity<Map<String, Object>> getAllUsers() {
        Map<String, Object> response = new HashMap<>();

        try {
            List<User> users = userService.getAll();

            response.put("success", true);
            response.put("data", users);
            response.put("count", users.size());

            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "获取用户信息失败: " + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }
    }
}

